package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.constant.UserConstants;
import com.usian.model.article.pojo.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WemediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.AuthMapper;
import com.usian.user.service.ApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ApUserServiceImpl extends ServiceImpl<AuthMapper, ApUserRealname> implements ApUserService {

    @Resource
    private ApUserMapper apUserMapper;

    @Autowired
    private WemediaFeign wemediaFeign;

    @Autowired
    private ArticleFeign articleFeign;

    @Override
    public ResponseResult findByNameAndPage(AuthDto dto) {
        //判断参数是否为空
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查分页参数
        dto.checkParam();
        //添加搜索条件
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getStatus()!=null, ApUserRealname::getStatus,dto.getStatus());
        //查询分页
        Page page = new Page(dto.getPage(), dto.getSize());
        //分页
        IPage page1 = page(page,queryWrapper);
        //封装参数
        PageResponseResult responseResult=new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        //取出分页数据  放入result 返回result
        responseResult.setData(page1.getRecords());
        return responseResult;
    }
    @GlobalTransactional
    @Override
    public ResponseResult updateStatusById(AuthDto dto, Integer status) {
        //检查参数
        if(dto==null || dto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查状态
        if(checkStatus(status)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.修改ap_user_realname 状态
         //2.1创建ApUserRealName对象 用来保存需要修改传递的参数
        ApUserRealname userRealname = new ApUserRealname();
        //用户的id和状态设置进来
        userRealname.setId(dto.getId());
        userRealname.setStatus(status);
        //2.2判断dto和状态设置进来
        if(dto.getMsg()!=null){
            userRealname.setReason(dto.getMsg());
        }
        //2.3更新状态
        updateById(userRealname);

        //3.添加自媒体人的账号和添加作者的信息
        if(status.equals(UserConstants.PASS_AUTH)){
            ResponseResult responseResult=createWemediaAndAuthor(dto);
            if(responseResult!=null){
                return responseResult;
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    //3.添加自媒体人的账号
    public ResponseResult createWemediaAndAuthor(AuthDto dto){
        //3.1查询ApUserRealName信息 根据dto中提供的信息来进行查询
        ApUserRealname userRealname=getById(dto.getId());
        //3.2根据userId 查询ApUser的相关数据
        ApUser apUser=apUserMapper.selectById(userRealname.getUserId());
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //3.3根据ApUser中的用户名查询自媒体账号是否存在
        WmUser wmUser=wemediaFeign.findByName(apUser.getName());
        if(wmUser==null){
            //3.4添加自媒体账号
            wmUser=new WmUser();
            //设置ApUserId
            wmUser.setApUserId(apUser.getId());
            wmUser.setCreatedTime(new Date());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setName(apUser.getName());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setStatus(9);
            wmUser.setPhone(apUser.getPhone());
            wemediaFeign.save(wmUser);
        }
        //4.添加作者
        createAuthor(wmUser);
        return null;
    }
    //添加作家号
    public void createAuthor(WmUser wmUser){
        //1.根据userId查询当前用户是否有作者号
        ApAuthor apAuthor=articleFeign.findByUserId(wmUser.getApUserId());
        if(apAuthor==null){
            //创建作家号
            apAuthor=new ApAuthor();
            apAuthor.setName(wmUser.getName());
            apAuthor.setType(UserConstants.AUTHOR_TYPE);
            apAuthor.setCreatedTime(new Date());
            apAuthor.setUserId(wmUser.getApUserId());
            articleFeign.save(apAuthor);
        }
    }
    /*
    * 状态检查
    * */
    public boolean checkStatus(Integer status){
        if(status!=null){
            return false;
        }
        return true;
    }
}
